#include "../../common/net.hpp"
#include "../../common/dispatcher.hpp"
#include "../../server/rpc_router.hpp"

void Add(const Json::Value& para, Json::Value& result)
{
    int num1 = para["num1"].asInt();
    int num2 = para["num2"].asInt();
    result = num1 + num2;
}

int main()
{
    MyRpc::server::RpcRouter::ptr rpc_router = std::make_shared<MyRpc::server::RpcRouter>();

    // 创建一个方法描述
    MyRpc::server::MethodFactory method;
    method.setMethodName("Add");
    method.setParam("num1", MyRpc::server::ParaType::INTEGRAL);
    method.setParam("num2", MyRpc::server::ParaType::INTEGRAL);
    method.setResultType(MyRpc::server::ParaType::INTEGRAL);
    method.setMethodCallBack(Add);

    // LOG(DEBUGLEVEL, "方法描述构建完成");
    // 把这个方法的描述注册到服务端的方法管理端
    MyRpc::server::ServiceDescribe::ptr it = method.build();
    rpc_router->registerMethod(it);

    // 创建服务端
    MyRpc::BaseServer::ptr server = MyRpc::ServerFactory::create(9090);

    // 把这个服务器管理的接口给dispatcher模块
    auto cb = std::bind(&MyRpc::server::RpcRouter::OnRpcMessage, rpc_router.get(), std::placeholders::_1, std::placeholders::_2);
    MyRpc::disp->Register<MyRpc::RpcRequest>(MyRpc::MType::REQ_RPC, cb);
    server->setMessageCallBack(std::bind(&MyRpc::Dispatcher::OnMessage, MyRpc::disp.get(), std::placeholders::_1, std::placeholders::_2));
    // 启动服务端
    server->start();

    return 0;
}